#include <QDir>
#include <QDomDocument>
#include <QApplication>

#include "infoplistparser.h"
#include "defines.h"

InfoPlistParser::InfoPlistParser(GeneratorOutputView &outputView, GeneratorData &generatorData)
	: m_outputView(outputView),
	m_generatorData(generatorData)
{
}

bool InfoPlistParser::parse()
{
	QString widgetDirectory = m_generatorData.widgetDirectory();
    QDir dir(widgetDirectory);

    dir.setFilter(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);

    // info.plist not found in widget directory
    if(!dir.exists(INFO_PLIST_FILE))
    {
        // find the first subdirectory and try it
        foreach(QFileInfo entry, dir.entryInfoList())
            if(entry.isDir()) {
            dir.cd(entry.baseName());
            break;
        }
    }

    if(!dir.exists(INFO_PLIST_FILE)) {
        QString errMsg = QString(ERR_FILE_NOT_FOUND).arg(INFO_PLIST_FILE);
        m_outputView.printOutput(errMsg);
        return false;
    }

    QMap<QString, QString> &infoPlistData = m_generatorData.infoPlistData();

    // read in info.plist
    QFile infoPlistFile(dir.filePath(INFO_PLIST_FILE));
    infoPlistData.insert(KEY_WIDGET_ROOT_DIR, dir.canonicalPath() + "/");

    // check the existence of icon file
    if(dir.exists(ICON_FILE)) {
        QString fullFileName(dir.canonicalPath() + "/" + ICON_FILE);
        QString shortFileName = fullFileName.mid(widgetDirectory.length());

        infoPlistData.insert(KEY_WIDGET_ICON_FILE, shortFileName);
    }

    if(!infoPlistFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QString errMsg = QString(ERR_FILE_OPEN_READ_FAILED_CANNOT_CONTINUE).arg(INFO_PLIST_FILE);
        m_outputView.printOutput(errMsg);
        return false;
    }

    QDomDocument infoPlistDocument;
    if(!infoPlistDocument.setContent(&infoPlistFile)) {
        QString errMsg = QString(ERR_FILE_PARSING_FAILED_CANNOT_CONTINUE).arg(INFO_PLIST_FILE);
        m_outputView.printOutput(errMsg);
        infoPlistFile.close();
        return false;
    }

    QDomElement infoPlistDocumentElement = infoPlistDocument.documentElement();
    QDomNode node = infoPlistDocumentElement.firstChild();
    QDomElement element;

    bool dictNodeFound = false;
    while(!node.isNull()) {
        if(node.isElement()) {
            element = node.toElement();
            if(element.tagName() == INFO_PLIST_TAG_DICT) {
                node = node.firstChild();
                dictNodeFound = true;
                break;
            }
            node = node.nextSibling();
        }
    }

    if(dictNodeFound) {
        QString key;
        QString value;
        while(!node.isNull()) {
            if(node.isElement()) {
                element = node.toElement();
                if(element.tagName() == INFO_PLIST_TAG_KEY) {
                    key = element.text();
                    node = node.nextSibling();
                    element = node.toElement();
                    if(key == INFO_PLIST_KEY_ALLOW_NETWORK_ACCESS || key == INFO_PLIST_KEY_MINI_VIEW_ENABLED) {
                        value = element.tagName();
                    }
                    else {
                        value = element.text();
                    }
                    infoPlistData.insert(key, value);
                }
            }
            node = node.nextSibling();
        }
    }
    else {
        QString errMsg = QString(ERR_XML_NODE_NOT_FOUND).arg(INFO_PLIST_TAG_DICT).arg(INFO_PLIST_FILE);
        m_outputView.printOutput(errMsg);
        return false;
    }

    QString msg = QString(MSG_FILE_PROCESSING_COMPLETED).arg(INFO_PLIST_FILE);
    m_outputView.printOutput(msg);

    return true;
}

